function Util(){
	this.baseUrl = 'http://lion.shihou.tv';
	//收到MVP
	this.mvp_receive_url = this.baseUrl + '/api/gift/receive_rank?gift_id=1070';
	this.mvp_receive_data_array = [];
	this.mvp_receive_data = {};
	//送出MVP
	this.mvp_send_url = this.baseUrl + '/api/gift/send_rank?gift_id=1070';
	this.mvp_send_data_array = [];
	this.mvp_send_data = {};
	//收到ACE
	this.ace_receive_url = this.baseUrl + '/api/gift/receive_rank?gift_id=1071';
	this.ace_receive_data_array = [];
	this.ace_receive_data = {};
	//送出ACE
	this.ace_send_url = this.baseUrl + '/api/gift/send_rank?gift_id=1071';
	this.ace_send_data_array = [];
	this.ace_send_data = {};

	
	this.init();
}
Util.prototype={
	init: function(){
		
	},
	setRankList: function(page){
		var self = this;
		var roomSkip = '';
		var min = 0;
		var now = 0;
		//绑定榜单
		//收到MVP
		self.log("收到MVP")
		self.requestRemoteDataJson(self.mvp_receive_url, {}, function(result){
			if(self.parseResult(result, 'error') == 0){
				var resultData = self.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					self.mvp_receive_data_array[i] = user;
					now = i;
					if(i == 9){
						break;
					}
				}
			}
		}, false);
		min = now;
		//荣耀之星
		self.log("送出MVP")
		self.requestRemoteDataJson(self.mvp_send_url, {}, function(result){
			if(self.parseResult(result, 'error') == 0){
				var resultData = self.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					self.mvp_send_data_array[i] = user;
					now = i;
					if(i == 9){
						break;
					}
				}
			}
		}, false);
		if(min > now){
			min = now;	
		}
		var phbmvp13 = '';
		var phbmvp410 = '';
		//排行榜赋值
		for(var i = 0;i < min+1;i++){
			var shihou = self.mvp_receive_data_array[i];
			var rongyao = self.mvp_send_data_array[i];
			if(page == 0){
				roomSkip = self.parseResult(shihou, 'user', 'room', 'jump');
			}else{
				roomSkip = 'http://event.m.shihou.tv/lion_module_share_temp/share?room_id='+self.parseResult(shihou, 'user', 'room', 'id');
			}
			if(i < 3){
				phbmvp13 += self.setRankListHTML((i+1), 1, roomSkip, self.parseResult(shihou, 'user', 'nick_name'), self.parseResult(shihou, 'user', 'avatar'), self.parseResult(shihou, 'user', 'room', 'vdo_status'), '收到'+self.resetTotal(self.parseResult(shihou, 'total'))+'个');
				phbmvp13 += self.setRankListHTML((i+1), 2, '', self.parseResult(rongyao, 'user', 'nick_name'), self.parseResult(rongyao, 'user', 'avatar'), self.parseResult(rongyao, 'user', 'room', 'vdo_status'), '送出'+self.resetTotal(self.parseResult(rongyao, 'total'))+'个');	
			}else{
				phbmvp410 += self.setRankListHTML((i+1), 1, roomSkip, self.parseResult(shihou, 'user', 'nick_name'), self.parseResult(shihou, 'user', 'avatar'), self.parseResult(shihou, 'user', 'room', 'vdo_status'), '收到'+self.resetTotal(self.parseResult(shihou, 'total'))+'个');
				phbmvp410 += self.setRankListHTML((i+1), 2, '', self.parseResult(rongyao, 'user', 'nick_name'), self.parseResult(rongyao, 'user', 'avatar'), self.parseResult(rongyao, 'user', 'room', 'vdo_status'), '送出'+self.resetTotal(self.parseResult(rongyao, 'total'))+'个');
			}
		}
		$('#phbmvp13').html(phbmvp13);
		$('#phbmvp410').html(phbmvp410);



		//魅力之星
		self.log("收到ACE");
		self.requestRemoteDataJson(self.ace_receive_url, {}, function(result){
			if(self.parseResult(result, 'error') == 0){
				var resultData = self.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					self.ace_receive_data_array[i] = user;
					now = i;
					if(i == 9){
						break;
					}
				}
			}
		}, false);
		min = now;
		//真爱之星
		self.log("送出ACE");
		self.requestRemoteDataJson(self.ace_send_url, {}, function(result){
			if(self.parseResult(result, 'error') == 0){
				var resultData = self.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					self.ace_send_data_array[i] = user;
					now = i;
					if(i == 9){
						break;
					}
				}
			}
		}, false);
		if(min > now){
			min = now;	
		}
		var phbace13 = '';
		var phbace410 = '';
		//排行榜赋值
		for(var i = 0;i < min+1;i++){
			var meili = self.ace_receive_data_array[i];
			var love = self.ace_send_data_array[i];
			if(page == 0){
				roomSkip = self.parseResult(meili, 'user', 'room', 'jump');
			}else{
				roomSkip = 'http://event.m.shihou.tv/lion_module_share_temp/share?room_id='+self.parseResult(meili, 'user', 'room', 'id');
			}
			if(i < 3){
				phbace13 += self.setRankListHTML((i+1), 1, roomSkip, self.parseResult(meili, 'user', 'nick_name'), self.parseResult(meili, 'user', 'avatar'), self.parseResult(meili, 'user', 'room', 'vdo_status'), '收到'+self.resetTotal(self.parseResult(meili, 'total'))+'个');
				phbace13 += self.setRankListHTML((i+1), 2, '', self.parseResult(love, 'user', 'nick_name'), self.parseResult(love, 'user', 'avatar'), self.parseResult(love, 'user', 'room', 'vdo_status'), '送出'+self.resetTotal(self.parseResult(love, 'total'))+'个');
			}else{
				phbace410 += self.setRankListHTML((i+1), 1, roomSkip, self.parseResult(meili, 'user', 'nick_name'), self.parseResult(meili, 'user', 'avatar'), self.parseResult(meili, 'user', 'room', 'vdo_status'), '收到'+self.resetTotal(self.parseResult(meili, 'total'))+'个');
				phbace410 += self.setRankListHTML((i+1), 2, '', self.parseResult(love, 'user', 'nick_name'), self.parseResult(love, 'user', 'avatar'), self.parseResult(love, 'user', 'room', 'vdo_status'), '送出'+self.resetTotal(self.parseResult(love, 'total'))+'个');
			}
		}
		$('#phbace13').html(phbace13);
		$('#phbace410').html(phbace410);
	},
	setRankListHTML: function(index, num, skipRoom, nick_name, avatar, kaibo_status, value){
		var html = '';
		var liClass = ''; //主播还是用户
		var aClass = 'liveNo'+index;//名次class
		var avatarHTML = '';
		var kaiboHTML = '';
		var rankIcon = '';
		
		if(index < 4){
			rankIcon = '<img src="static/img/icon'+index+'.png" />';
		}else{
			rankIcon = index;
			aClass = 'liveNo4';
		}
		
		if(kaibo_status == 1){//如果开播
			kaiboHTML = '    <em class="liveIcon"></em>'+
						'	 <em class="liveOpacity"></em>'+
                        '    <div class="loader-inner line-scale-pulse-out">'+
                        '        <div></div>'+
                        '        <div></div>'+
                        '        <div></div>'+
                        '        <div></div>'+
                        '        <div></div>'+
                        '    </div>';
		}
		if(num == 1){//第1排
			liClass = 'anchor';
			clas = 'font1';
		    avatarHTML = '<span class="liveHead">'+
                         '   	<span class="liveWrap">'+
                         			 kaiboHTML+
                         '           <img src="'+avatar+'"/>'+
                         '       </span>'+
                         '   </span>'+
                         '	<span class="liveName">'+nick_name+'</span>'+
                         '  <span class="liveNum">'+value+'</span>'+
                         '<span class="liveRanking">'+rankIcon+'</span>';    
            html = '<li class="'+liClass+'">'+
			   '	<a href='+skipRoom+' class="'+aClass+'">'+
	               	avatarHTML+
	           '	</a>'+    
	           '</li>';
		}else{//第2排
			liClass = 'user';
		    avatarHTML = '<span class="liveHead">'+
		    			 '	<img src="'+avatar+'"/></span>'+
                    	 '	<span class="liveName">'+nick_name+'</span>'+
                         '<span class="liveNum">'+value+'</span>';
            html = '<li class="'+liClass+'">'+
			   '	<a href="javascript:void();" class="'+aClass+'">'+
	               	avatarHTML+
	           '	</a>'+    
	           '</li>';             
		}
		
		return html;
	},
	setUserRank: function(user_id){
		
		var self = this;
		//绑定用户排名
		//荣耀榜排名
		self.requestRemoteDataJson(self.mvp_receive_url, {}, function(result){
			if(self.parseResult(result, 'error') == 0){
				var resultData = self.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					self.mvp_receive_data['"'+self.parseResult(user, 'user', 'id')+'"'] = (i+1);
				}
				$('#mvp1').html('荣耀榜排名：'+setUR(self.mvp_receive_data, user_id));		
			}
		}, true);
		//助攻榜排名
		self.requestRemoteDataJson(self.mvp_send_url, {}, function(result){
			if(self.parseResult(result, 'error') == 0){
				var resultData = self.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					self.mvp_send_data['"'+self.parseResult(user, 'user', 'id')+'"'] = (i+1);
				}
				$('#mvp2').html('助攻榜排名：'+setUR(self.mvp_send_data, user_id));		
			}
		}, true);
		//枪神榜排名
		self.requestRemoteDataJson(self.ace_receive_url, {}, function(result){
			if(self.parseResult(result, 'error') == 0){
				var resultData = self.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					self.ace_receive_data['"'+self.parseResult(user, 'user', 'id')+'"'] = (i+1);
				}
				$('#ace1').html('枪神榜排名：'+setUR(self.ace_receive_data, user_id));		
			}
		}, true);
		//助攻榜排名
		self.requestRemoteDataJson(self.ace_send_url, {}, function(result){
			if(self.parseResult(result, 'error') == 0){
				var resultData = self.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					self.ace_send_data['"'+self.parseResult(user, 'user', 'id')+'"'] = (i+1);
				}
				$('#ace2').html('助攻榜排名：'+setUR(self.ace_send_data, user_id));		
			}
		}, true);
		
		function setUR(obj, user_id){
			var resu = obj['"'+user_id+'"'];
			if(resu == undefined){
				return '未上榜';
			}
			return resu;
		}
		
	},
	resetTotal: function(total){
		if(total == undefined)
			return 0;
		return parseInt(total/6);
	},
	//获取url中的参数
    getUrlParam: function(name, url) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        url = url || window.location.search.substr(1).match(reg);  //匹配目标参数
        if (url != null) return unescape(url[2]); return null; //返回参数值
    },
    getUrlParams: function(){
    	var url = window.location.href;  //匹配目标参数
    	if(url != null)
    		return url.substr(url.indexOf("?"), url.length);
    },
    parseResult:function(){//解析结果， 如果不存在则返回''
		var size = arguments.length;
		if(size == 0){
			return '';
		}
		var result = arguments[0];
		for(var i = 1; i < size; i++){
			if(result == undefined){
				return '';
			}
			var argu = arguments[i];
			if(i == size){
				return argu;
			}
			result = result[argu];
		}
		return result;
	},
	requestRemoteDataJson: function(url, data, callback, asyn){
		this.logURL(url);
		//this.logParameter(data);
		var begin = (new Date()).getTime();
		$.ajax({
	        url: url,
	        type: 'GET',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	log:function(data){
		console.log(data);
	},
	logURL:function(url){
		console.log("url --> "+url);
	},
	logParameter:function(data){
		console.log("parameter --> "+JSON.stringify(data));
	},
	logResult:function(result){
		console.log("result --> "+JSON.stringify(result));
	}
}
